package com.java01.apache.commons.collections;

import java.util.Iterator;
import java.util.Set;

import org.apache.commons.collections.Bag;
import org.apache.commons.collections.bag.HashBag;

public class HashBagSnippet {

	public static void main(String[] args) {
		Bag bag = new HashBag();
		bag.add("A", 2); // add 2 copies of "A"
		bag.add("B", 3); // add 3 copies of "B"
		bag.add("C", 4); // add 4 copies of "C"
		bag.remove("B", 2);  // removes 2 copies of "B"
		bag.add("B", 1); // add 1 copy of "B"
		
		Iterator it=bag.iterator();
		while(it.hasNext()){
			Object key = it.next();
			System.out.print(key);
		}
		
		System.out.println();
		
		Set keySet=bag.uniqueSet();
		it=keySet.iterator();
		while(it.hasNext()){
			Object key = it.next();
			System.out.println(key+" has "+bag.getCount(key)+" copies.");
		}
	}
}
